import { Component, OnInit, Input, Output, EventEmitter, OnChanges, SimpleChanges } from '@angular/core';
import { CoreMessageService } from '../../../core/services/core-message.service';


interface ItemData {
  name: string;
  age: number;
  address: string;
}

@Component({
  selector: 'app-table-data',
  templateUrl: './table-data.component.html',
  styleUrls: ['./table-data.component.scss']
})
export class TableDataComponent implements OnInit, OnChanges {
  @Output() currentItem = new EventEmitter<any>();
  @Input() data = null;
  keys = [];
  selectindex = -1;
  // values = [];
  constructor(private msgService: CoreMessageService) { }

  ngOnInit() {
      // 获取到数据后提取key值！
      if (this.data[0].detail){
        const rex = /^[a-zA-Z]/g;
        this.keys = [];
        const keyArray = Object.keys(this.data[0].detail);
        this.keys = keyArray.filter(i => {
          return !rex.test(i);
        });
      }
  }

  // TODO:展示详情------>>>modal框(modal框的打开方式只有使用发布和订阅机制吗？)
  showDetail(e, i ){
    this.selectindex = i;
    this.currentItem.emit(e);
    // this.msgService.showModal(e);
  }
  mouseenter(e) {
   // this.currentItem.emit(e);
   }
  mouseleave(e) {
   // this.currentItem.emit(false);
  }
  ngOnChanges(changes: SimpleChanges): void {
    if (this.data[0].detail){
      // 每次数据变更，都清除掉选中状态
      this.selectindex = -1;
      const rex = /^[a-zA-Z]/g;
      this.keys = [];
      const keyArray = Object.keys(this.data[0].detail);
      this.keys = keyArray.filter(i => {
        return !rex.test(i);
      });

      // for (let [key, value] of Object.entries(this.data[0].detail)) {
      //   this.keys.push(key);
      // }
    }
  }

}
